package com.ag.improvedjava.utils.comparators;

import java.util.Comparator;

import com.ag.improvedjava.utils.Function;

public final class ProjectionComparator<T, K> implements Comparator<T> {
	
	private final Function<T, K> keySelector; 
    private final Comparator<K> comparer;
    
	public ProjectionComparator(Function<T, K> keySelector,
			Comparator<K> comparer) {
		this.keySelector = keySelector;
		this.comparer = comparer;
	}

	@Override
	public int compare(T x, T y) {
		K keyX = keySelector.execute(x);
		K keyY = keySelector.execute(y);
		return comparer.compare(keyX, keyY);
	}
}
